import { getToken } from './deepin-auth'

const API_BASE = 'https://store-api.deepin.org/v1'

export const fetchReviews = async (params = {}) => {
  try {
    const response = await fetch(`${API_BASE}/reviews?${new URLSearchParams(params)}`, {
      headers: {
        'Authorization': `Bearer ${getToken()}`
      }
    })
    
    if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
    
    const { data } = await response.json()
    return data.map(item => ({
      id: item.id,
      packageName: item.attributes.package_name,
      status: item.attributes.status,
      rejectCode: item.attributes.reject_code
    }))
  } catch (error) {
    console.error('审核列表获取失败:', error)
    throw error
  }
}

export const bulkApprove = async (packageHashes) => {
  return fetch(`${API_BASE}/reviews/bulk_approve`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${getToken()}`
    },
    body: JSON.stringify({ packages: packageHashes })
  })
}